package comp
{
	use namespace comp_layout;

	import comp.Interface.IDispose;
	import comp.style.css.CssAchieve;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * 数据列表
	 * 显示单列数据，支持自定义模板
	 */
	public class List extends ListBase
	{
		include "style/BorderStyles";//包含样式
		include "style/SkinStyle" //皮肤属性
		include "style/PaddingStyles";//包含样式

		public function List()
		{
			super();
			this._themeName="comp::List";
			super.onItemBind=_onMyItemBind;
			this.addEventListener(MouseEvent.MOUSE_WHEEL,onWheelHandler);
		}
		
		//********************* 样式 *********************
		private var _overflow:String;//="visible";	//visible,hidden,scroll //,auto	
		private var _scrollbarMarginRight:Number;//滚动条MarginRight
		private var _scrollbarMarginTop:Number;//滚动条MarginTop
		private var _scrollExpand:Number;//滚动区域扩展
		private var _scrollBarClass:String;//滚动条样式内
		
		//********************* 私有成员 *********************
		private var _onItemBinding:Function;
		protected var _scrollbar:ScrollBar;//滚动条
		private var _container:Sprite;//类容容器
		
		//************** override **********************
		/**
		 * 结构必须为:
		 * function itemBind(i:uint,data:*):DisplayObject
		 */
		public override function set onItemBind(value:Function):void
		{
			_onItemBinding=value;
		}

		//protected	
		override	protected function createChildren():void
		{
			removeAllChild();
			
			if(this.data==null)
				return;
			
			var itda:*;
			for(var i:uint=0;i<data.length;i++)
			{
				if(onItemBind==null)
					return;
				itda=data[i];
				var d:DisplayObject=onItemBind(i,itda);
				if(d!=null)
				{
					d.name=getName(i);
					if(d is UIComponent)
						UIComponent(d).initialize();
					this.container.addChild(d);
					try{
						if(_onItemBinded!=null)
							_onItemBinded(i,itda,d);
						
					}catch(e:*){}
				}
			}
		}
		
		override protected function merge():void
		{
			var ml:Number=getStyle("marginLeft");
			var mr:Number=getStyle("marginRight");
			var mt:Number=getStyle("marginTop");
			var mb:Number=getStyle("marginBottom");
			
			var pl:Number=getStyle("paddingLeft");
			var pt:Number=getStyle("paddingTop");
			var pr:Number=getStyle("paddingRight");
			var pb:Number=getStyle("paddingBottom");
			
			var lx:Number=0,ly:Number=0;//上个对像x和上个对像y
			var i:uint=0;
			var child:*;
			var cheight:Number,tMax:Number=0;//=height-this.getStyle("marginBottom");//内容宽度
			
			container.x=pl;
			container.y=pt;
			
			//垂直类按垂直顺序从上向下排列布局元素，在元素和围绕元素顺序的可选填充之间带有可选间隙。
			var num:int=this.container.numChildren;
			for(i=0;i<num;i++)
			{
				child=container.getChildAt(i);
				if(child.visible==true)// ||  !(child is  ScrollBar)
				{
					child.x=pl;
					child.y=ly+mt;	
					
					ly=child.y+child.height+mb;
					
					if(child is UIComponent)
					{
						if(child.visible==true) 
						{
							cheight=child.y+(child.height==0?child.spriteHeight:child.height);
						}
						//cheight=s.y+(s.maxHeight==0?s.spriteHeight:s.maxHeight);
					}
					else if(child.visible==true)
					{
						cheight=child.y+child.height;
					}
					
					if(tMax<=cheight)
						tMax=cheight;
				}
			}
			_maxHeight=tMax+pb;
			
			if(this._autoH) //自动高
				this._myHeight=this._maxHeight;
			
			onSizeChangeHandle();
			CssAchieve.mergeBackGrounde(this);
			CssAchieve.mergeTemplate(this);
		}
		
		/**
		 * 不能用这个方法添加对象
		 */
		override public function addChild(child:DisplayObject):DisplayObject
		{
			return null;
		}
		
		/**
		 * 不能用这个方法添加对象
		 */
		override public function addChildAt(child:DisplayObject, index:int):DisplayObject
		{
			if(child==this.currentSkin)
				super.addChildAt(child,index);
			else				
			{
				addingChild(child);
				container.addChildAt(child,index);
				addedChild(child);
				if(this.initialized) //如果已经初使化需要计算合并
					merge();
			}			
			return  child;
		}
		
		/**
		* 移除所有子对像
		*/
		 override public function removeAllChild():void
		{
			 while(container.numChildren>0)
			 {
				 var dis:*=container.removeChildAt(0);
				 if(dis is IDispose)
					 dis.dispose();
			 }
		}
		 
		 /**
		 * 得到选择项的项
		 */
		 override public function get selectedItem():Object
		 {
			 if(this.numChildren>this.selectedIndex && this.selectedIndex>=0)
				 return container.getChildByName(getName(selectedIndex));
			 else
				 return null;
		 }
		 
		 /**
		  * 得到选择的所有项
		  */
		 override public function get selectedItems():Array
		 {
			 var j:uint=0;
			 var obj:Object;
			 var result:Array=new Array;
			 for(var i:* in this.selectedIndexs)
			 {
				 j=selectedIndexs[i];
				 obj=container.getChildByName(getName(j));
				 if(obj)
					 result.push(obj);
			 }
			 
			 return result;
		 }	
		 
		 /**
		  * 上次选中的单项
		  */
		 override public function get lastSelectedItem():Object
		 {
			 if(container.numChildren>this.lastSelected && this.lastSelected>=0)
				 return container.getChildByName(getName(lastSelected));
			 else
				 return null;
		 }
		 
		 
		 override public function  getChildAt(index:int):DisplayObject
		 {
			 return container.getChildAt(index);
		 }
		 
		 override public function getChildByName(name:String):DisplayObject
		 {
			 return container.getChildByName(name);
		 }
		 
		 override public function getChildIndex(child:DisplayObject):int
		 {
			 return container.getChildIndex(child);
		 }
		 
		 override public function get numChildren():int
		 {
			 return container.numChildren;
		 }
		 
		 override public function removeChildAt(index:int):DisplayObject
		 {
			 return container.removeChildAt(index);
		 }
		 
		 override public function removeChild(child:DisplayObject):DisplayObject
		 {
			 return container.removeChild(child);
		 }
		 
		 override public function setChildIndex(child:DisplayObject, index:int):void
		 {
			 container.setChildIndex(child,index);
		 }
		//************** 私有 **********************
		
		 comp_layout function onSizeChangeHandle():void
		{
	
			var overf:String=getStyleDefault("overflow","auto").toLocaleLowerCase();
			
			if(overf=="visible") //如果是不显示
				return;
			
			var pl:Number=getStyle("paddingLeft");
			var pt:Number=getStyle("paddingTop");
			var pr:Number=getStyle("paddingRight");
			var pb:Number=getStyle("paddingBottom");
			
			var containerheight:Number=height-pt-pb; 
			
			if(container.scrollRect==null)
				container.scrollRect=new Rectangle(0,0,width-pl-pr,containerheight);//如果指定了宽度
			
			if(overf=="scroll" || overf=="auto" )
			{
				if(containerheight<_maxHeight)
					showScroll();
				else
				{
					hideScroll();
					if(container.scrollRect!=null)
						container.scrollRect=null;//new Rectangle(0,0,width,height);//如果指定了宽度
				}
			}
		}
		
		/**
		 * 隐藏滚动条
		 */
		protected function hideScroll():void
		{
			if(_scrollbar)
				_scrollbar.visible=false;
		}
		
		/**
		 * 创建滚动条
		 */
		protected function showScroll():void
		{
			if(!_scrollbar && container.scrollRect!=null)
			{
				var sexp:Number=this.getStyleDefault("scrollExpand",0);
				_scrollbar=new ScrollBar;
				_scrollbar.height=container.scrollRect.height;
				_scrollbar.maxHeight=_maxHeight;
				_scrollbar.tag=this.container;	
				_scrollbar.className=this.getStyle("scrollBarClass");						
				_scrollbar.onScroll=function(n:Number):void{
					var r:Rectangle=this.tag.scrollRect;
					if(r)
					{
						r.y=n-sexp;
						var r1:Rectangle=r;
						this.tag.scrollRect=r1;
					}
					
				}
				
				_scrollbar.x=this.width-this.getStyle("scrollbarMarginRight");
				_scrollbar.y=this.getStyle("scrollbarMarginTop");
				super.addChildAt(_scrollbar,0);
			}
			
			if(_scrollbar)
			{
				
				var yy:Number=_scrollbar.scrollY;
				var n:Number=yy*(_scrollbar.maxHeight/(_maxHeight));
				
				super.setChildIndex(_scrollbar,super.numChildren-1);
				
				_scrollbar.height=container.scrollRect.height;
				_scrollbar.visible=true;
				_scrollbar.x=this.width+this.getStyle("scrollbarMarginRight");
				_scrollbar.maxHeight=_maxHeight;
				_scrollbar.onSizeChange();
				
				_scrollbar.scroll(n);

				super.setChildIndex(_scrollbar,super.numChildren-1);
			}
			
		}
		
		/**
		 * 滚轮事件
		 */
		protected function onWheelHandler(e:MouseEvent):void
		{
			if(_scrollbar)
				_scrollbar.scroll(-e.delta*2);
		}
		
		/**
		 * 数据绑定事件
		 */		
		private function _onMyItemBind(index:int,data:*):DisplayObject
		{
			var d:*;
			if(this._onItemBinding!=null)
			{
				d=_onItemBinding(index,data);
			}else
			{
				d=new Label();
				d.style=this.style;
				if(this.showField)
					d.text=data[showField];
				else
					d.text=data;
			}
			return d;
		}


		//******************** 样式属性 **************************
		
		/**
		 * 类容容器
		 */
		public function get container():Sprite
		{
			if(_container==null)
			{
				_container=new Sprite();
				super.addChild(_container);
			}
			return _container;
		}
		
		/**
		 *  滚动条的显示设置 visible,hidden,scroll //,auto	
		 */
		public function get overflow():String
		{
			return _overflow;
		}

		/**
		 * 滚动条的显示设置 visible,hidden,scroll //,auto	
		 */
		public function set overflow(value:String):void
		{
			_overflow = value;
		}

		/**
		 * 滚动条MarginRight
		 */
		public function get scrollbarMarginRight():Number
		{
			return _scrollbarMarginRight;
		}

		/**
		 * @private
		 */
		public function set scrollbarMarginRight(value:Number):void
		{
			_scrollbarMarginRight = value;
		}

		/**
		 * 滚动条MarginTop
		 */
		public function get scrollbarMarginTop():Number
		{
			return _scrollbarMarginTop;
		}

		/**
		 * @private
		 */
		public function set scrollbarMarginTop(value:Number):void
		{
			_scrollbarMarginTop = value;
		}


		/**
		 * 滚动条样式
		 */
		public function get scrollBarClass():String
		{
			return _scrollBarClass;
		}
		
		/**
		 * @private
		 */
		public function set scrollBarClass(value:String):void
		{
			_scrollBarClass = value;
		}
		
		public function get scrollExpand():Number
		{
			return _scrollExpand;
		}
		
		/**
		 * 滚动区域扩展
		 * 
		 */
		public function set scrollExpand(value:Number):void
		{
			_scrollExpand = value;
		}

	}
}